#include "led.h"
#include "type.h"

void delay(volatile int d) {
  while (d--)
    ;
}

void led_test() {
  int val = 0;
  int tmp;
  int count = 10;

  GPBCON &= ~((3 << 10) | (3 << 12) | (3 << 14) | (3 << 16));
  GPBCON |= ((1 << 10) | (1 << 12) | (1 << 14) | (1 << 16));

  while (count--) {
    tmp = ~val;
    tmp &= 7;
    GPBDAT &= ~(7 << 4);
    GPBDAT |= (tmp << 4);
    delay(10000);
    val++;
    if (val == 8)
      val = 0;
  }
}

void led_switch(uint8 id, uint val) {
  switch (id) {
  case 1:
    if (val & (1 << 0)) {
      GPBDAT |= (1 << 5);
    } else {
      GPBDAT &= ~(1 << 5);
    }
    break;
  case 2:
    if (val & (1 << 2)) {
      GPBDAT |= (1 << 6);
    } else {
      GPBDAT &= ~(1 << 6);
    }
    break;
  case 3:
    if (val & (1 << 3)) {
      GPBDAT |= (1 << 7);
    } else {
      GPBDAT &= ~(1 << 7);
    }
    break;
  case 4:
    if (val & (1 << 11)) {
      GPBDAT |= (1 << 8);
    } else {
      GPBDAT &= ~(1 << 8);
    }
    break;
  default:
    break;
  }
}
